package com.xyw.code.study20200813.test;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/8/31
 * Time: 下午2:47
 * Email: 1328312923@qq.com
 * Description: No Description
 **/
@Slf4j
public class MyList<T> {
    private Object[] attr = new Object[16];
    private Integer length = 0;

    /**
     * 扩容
     * @param size
     */
    private void sizeOpen(Integer size){
        if(size>attr.length){
            Object[] attr2 = null;
            if(size<attr.length*2){
                attr2 = new Object[attr.length*2];
            }else{
                attr2 = new Object[size];
            }
            System.arraycopy(attr,0,attr2,0,attr.length);
        }
    }

    private void add(T ele){
        if (ele!=null){
            try{
                //看看需不需要扩容
                sizeOpen(length+1);
                attr[length] = ele;
            }catch (Exception e){
                log.error("新增出错");
                e.printStackTrace();
            }finally {
                this.length++;
            }
        }

    }

    private void remove(T ele){
        if (ele!=null){
            try{
                int nullSize = 0;
                //看看需不需要扩容
                for(int i=0;i<length;i++){
                    if(ele.equals(attr[i])){
                        attr[i] = null;
                        nullSize ++;
                    }
                }
                this.length = this.length - nullSize;
                removeNull(attr);
            }catch (Exception e){
                log.error("移除出错");
                e.printStackTrace();
            }
        }
    }

    private void removeNull(Object[] attr){
        int z = 0;
        for(int i=0;i<length;i++){
            if(attr[i]==null ){
                z++;
            }
            if(z!=0 && (i+z)<(attr.length-1)){
                attr[i] = attr[i+z];
            }
            if(i+z>(attr.length-1)){
                break;
            }
        }
        for(int i=length-1;i<attr.length;i++){
            attr[i] = null;
        }
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this.attr);
    }

    public Integer getLength() {
        return this.length;
    }

    public void fastRemove(int index) {
        int numMoved = length - index - 1;
        if (numMoved > 0)
            System.arraycopy(this.attr, index+1, this.attr, index,
                    numMoved);
        this.attr[--length] = null; // clear to let GC do its work
    }

    public void reverse() {
        for(int i=0;i<length/2;i++){
            Object e = attr[i];
            attr[i] = attr[length-1-i];
            attr[length-1-i] = e;
        }
    }

    public void sort() {
        for(int i=0;i<length;i++){
            //排序
            for(int z=i+1;z<length;z++){
                Integer o1 = (Integer) attr[i];
                Integer o2 = (Integer) attr[z];
                if(o1>o2){
                    attr[i] = attr[z];
                    attr[z] = o1;
                }
            }
        }
    }



    public static void main(String[] args) {
        MyList<Integer> list = new MyList<>();
        list.add(4444);
        list.add(5555);
        list.add(1111);
        list.add(2222);
        list.add(3333);
        list.add(6666);
//        list.fastRemove(4);
        list.sort();




        Integer[] attr1 = new Integer[6];
        for(int i=0;i<attr1.length;i++){
            attr1[i] = i;
        }
        System.arraycopy(attr1,2,attr1,1,3);
        System.out.println(JSONObject.toJSONString(attr1));


        //set底层 hashMap
        //LinedList 底层Node
    }
}
